(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("signonService", signonService);
	signonService.$inject = [ '$rootScope', '$q', '$locale', 'Constants', 'loggerFactory', 'storageService', 'ajaxServiceFactory', 'SYS_PARAMS'];
	function signonService($rootScope, $q, $locale, Constants, loggerFactory, storageService, ajaxServiceFactory, SYS_PARAMS) {
		var log = loggerFactory.getLogger("signonService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			loadMenus : {
				url : 'loadMenus.do',
				method : 'post'
			},
			loadUserInfo : {
				url : 'loadSignonInfo.do',
				method : 'post'
			},
			/*exit : {
				url : 'signoff.do',
				method : 'get'
			},*/
			getSiteCurrencies : {
				url : 'siteCurrencies.do',
				method : 'post'
			}
		});
		function getUserStatus() {
			var defer = $q.defer();
			ajaxService.getUserStatus().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getLocaleList() {
			var defer = $q.defer();
			ajaxService.getLocaleList().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function isAuthenticated() {
			var defer = $q.defer(), promises = [];
			ajaxService.loadUserInfo().then(function(response) {
				if (storageUserInfo(response)) {
					promises.push(ajaxService.loadMenus({"menuName":"APF-B"}));
					promises.push(ajaxService.getSiteCurrencies());
					$q.all(promises).then(function(responses) {
						storageService.saveMenuInfo(responses[0].data ? responses[0].data[1] || [] : []);
						storageService.saveMenuConfigInfo(responses[0].data ? responses[0].data[0] || [] : []);
						if (responses[1] && responses[1].data) {
							angular.forEach(responses[1].data, function(data, index, array){
								SYS_PARAMS.CCY_CONFIG[data.ccy] = data.decimalNum;
							});
						}
						defer.resolve();
					}, function(error) {
						log.debug(angular.toJson(error));
						defer.reject('load sys info error');
					});
				} else {
					defer.reject();
				}
			}, function() {
				defer.reject();
			});
			return defer.promise;
		}
		function exit() {
			storageService.clearAllFromSession();
			// storageService.clearAllFromLocal();
			delete $rootScope.user;
			delete $rootScope.site;
			delete $rootScope.menus;
			$rootScope.$emit(Constants.NOTIFY_REDIRECT, {
				url : 'signoff.do'
			});
			return $q.resolve();
			//return ajaxService.exit();
		}

		function afterSignon(response) {
			var defer = $q.defer(),promises=[];
			storageService.clearAllFromSession();
			// storageService.clearAllFromLocal();
			delete $rootScope.user;
			delete $rootScope.site;
			delete $rootScope.menus;
			if (response && response.data) {
				if (storageUserInfo(response)) {
					promises.push(ajaxService.loadMenus({"name":"APF"}));
					promises.push(ajaxService.getSiteCurrencies());
					$q.all(promises).then(function(responses) {
						storageService.saveMenuInfo(responses[0].data ? responses[0].data[1] || [] : []);
						storageService.saveMenuConfigInfo(responses[0].data ? responses[0].data[0] || [] : []);
						if (responses[1] && responses[1].data) {
							angular.forEach(responses[1].data, function(data, index, array){
								SYS_PARAMS.CCY_CONFIG[data.ccy] = data.decimalNum;
							});
						}
						defer.resolve();
					}, function(error) {
						log.debug(angular.toJson(error));
						defer.reject('load sys info error');
					});
				} else {
					defer.reject('load user info error');
				}
			} else {
				ajaxService.loadUserInfo().then(function(response) {
					if (storageUserInfo(response)) {
						promises.push(ajaxService.loadMenus({"name":"APF"}));
						promises.push(ajaxService.getSiteCurrencies());
						$q.all(promises).then(function(responses) {
							storageService.saveMenuInfo(responses[0].data ? responses[0].data[1] || [] : []);
							storageService.saveMenuConfigInfo(responses[0].data ? responses[0].data[0] || [] : []);
							if (responses[1] && responses[1].data) {
								angular.forEach(responses[1].data, function(data, index, array){
									SYS_PARAMS.CCY_CONFIG[data.ccy] = data.decimalNum;
								});
							}
							defer.resolve();
						}, function(error) {
							log.debug(angular.toJson(error));
							defer.reject('load sys info error');
						});
					} else {
						defer.reject('load user info error');
					}
				}, function() {
					defer.reject('load user info error');
				});
			}
			return defer.promise;
		}

		function storageUserInfo(response) {
			var siteInfo, userInfo, formatInfo, systemInfo;
			if (!response || !response.data || !angular.isObject(response.data)) {
				return false;
			}
			systemInfo = response.data.systemInfo;
			formatInfo = response.data.formatInfo;
			userInfo = response.data.userInfo;
			siteInfo = response.data.siteInfo;
			if (formatInfo) {
				angular.forEach(formatInfo, function(value, key) {
					if (angular.isString(value)) {
						formatInfo[key] = value.replace('SSS', 'sss');
					}
				});
				storageService.saveFormatInfo(formatInfo);
				angular.extend($locale.DATETIME_FORMATS, formatInfo);
			}
			if (systemInfo) {
				Constants.ENCODE_SEPARATOR = systemInfo.ENCODE_SEPARATOR;
				Constants.CSRF_TOKEN = systemInfo.CSRF_TOKEN;
				Constants.TRANSACTION_TOKEN = systemInfo.TRANSACTION_TOKEN;
			}
			storageService.saveSiteInfo(siteInfo);
			storageService.saveUserInfo(userInfo);
			storageService.saveSystemInfo(systemInfo);
			return true;
		}
		return {
			afterSignon : afterSignon,
			isAuthenticated : isAuthenticated,
			exit : exit
		};
	}
})(angular);